﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.MsQuestion;
using Ccit.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using Ccit.OA.Utils;
namespace Ccit.OA.Question
{
    public partial class qst_dataentry_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 ViewState["MSGIDS"] = "";
                _hidsRecid.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "0";
                _hidCurCount.Value = (null != Request.QueryString["curCount"]) ? Request.QueryString["curCount"].ToString() : "1";
                _hidType.Value = (null != Request.QueryString["type"]) ? Request.QueryString["type"].ToString() : "0";
                _hidOpenType.Value = (null != Request.QueryString["OpenType"]) ? Request.QueryString["OpenType"].ToString() : "";                
                if (_hidType.Value == "1")
                {
                    tdPage.Attributes["style"] = "display:none";
                }
                else
                {
                    QuestionBLL Bll = new QuestionBLL();
                    DataSet dSetR = Bll.GetQuestionsMesInfo("");
                    if (dSetR.Tables[0].Rows.Count > 0)
                    {
                        _hidsRecid.Value = dSetR.Tables[0].Rows[0][0].ToString();
                    }
                }
                ShowPage();
                //InitPage(); 
            }  
        }
        private void ShowPage()
        {
            QuestionBLL Bll = new QuestionBLL();
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            MsQuestionMes ms = Bll.GetQuestionsMessage(_hidsRecid.Value);
            string uId = SysInterface.GetUserIdsByRangeType(ms.range, ms.rangemark, ms.type);
            string[] uLen = uId.Split(',');
            for (int j = 0; j < uLen.Length; j++)
            {
                if (login.UserId == uLen[j])
                {
                    DataSet dSet = Bll.GetQuestionsMesInfo(_hidsRecid.Value);
                    if (dSet.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                        {
                            DataRow dRow = dSet.Tables[0].Rows[i];
                            ViewState["MSGIDS"] += (ViewState["MSGIDS"] == "") ? dSet.Tables[0].Rows[i][0].ToString() : "," + dSet.Tables[0].Rows[i][0].ToString();
                        }
                        _hidMaxCount.Value = dSet.Tables[0].Rows.Count.ToString();
                        initializeFirstPage(_hidsRecid.Value, _hidCurCount.Value);
                    }
                    btnSave.Attributes["style"] = "display:block";
                }               
            }
            Bll = null;
        }
        private void initializeFirstPage(string msgId, string curCount)
        {
            InitPage(); 
            EnableLink(curCount); 
            lblMsgCount.Text = _hidMaxCount.Value;
            lblCurCount.Text = curCount;  
        }
        //连接可用否
        private void EnableLink(string ocurCount)
        { 
            int curCount = int.Parse(ocurCount);
            int sumCount = 0; 
            sumCount = int.Parse(_hidMaxCount.Value);

            if (sumCount > 1)
            {
                if (curCount == 1)
                {
                    lnkFirst.Enabled = false;
                    lnkUp.Enabled = false;

                    lnkDown.Enabled = true;
                    lnkLast.Enabled = true;
                }
                else if (curCount == sumCount)
                {
                    lnkLast.Enabled = false;
                    lnkDown.Enabled = false;

                    lnkFirst.Enabled = true;
                    lnkUp.Enabled = true;
                }
                else
                {
                    lnkFirst.Enabled = true;
                    lnkUp.Enabled = true;
                    lnkDown.Enabled = true;
                    lnkLast.Enabled = true;
                }
            }
            else
            {
                lnkFirst.Enabled = false;
                lnkUp.Enabled = false;
                lnkLast.Enabled = false;
                lnkDown.Enabled = false;
            }
        }
        private void InitPage()
        {
                QuestionBLL Bll = new QuestionBLL();
                MsQuestionMes msMes = Bll.GetQuestions(_hidsRecid.Value.ToString());
                LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"]; 
                if (msMes != null)
                {
                    string range = SysInterface.GetUserIdsByRangeType(msMes.rangemark,msMes.range,msMes.state);
                    string[] rangework = range.Split(',');
                    for (int j = 0; j < rangework.Length; j++)
                    {
                        if (rangework[j] == login.UserId)
                        {
                            if (msMes != null)
                            {
                                lblTitle.Text = (msMes.title != "") ? msMes.title : "&nbsp;";
                                lblEndTime.Text = (msMes.endtime != "") ? msMes.endtime : "&nbsp;";
                                lblExplain.Text = (msMes.explain != "") ? msMes.explain : "&nbsp;";
                                lblType.Text = (msMes.type == "0") ? "匿名" : "实名";
                            }
                            Create();
                        }
                    }
                }
                Bll = null;  
        }
        private void Create()
        {
             QuestionBLL Bll = new QuestionBLL();
             DataSet dSet = Bll.GetQuestion(_hidsRecid.Value, "5");
             LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"]; 
             if (dSet.Tables[0].Rows.Count > 0)
             {
                 _hidCount.Value = dSet.Tables[0].Rows.Count.ToString();
                 for (int i = 0; i < dSet.Tables[0].Rows.Count ;i++ )
                 {
                     DataRow dRow = dSet.Tables[0].Rows[i];
                     _hidWTId.Value += (_hidWTId.Value.ToString() == "") ? dRow["Qst_A2_10"].ToString() : "," + dRow["Qst_A2_10"].ToString();
                     CreateTable(i, dRow, login.UserId);
                 } 
                 //_hidsRecid.Value = dSet.Tables[0].Rows[0][0].ToString();                 
             }
             Bll = null;
        } 
        private void CreateTable(int i, DataRow dRow,string userId)
        {
            int iLine = i;
            HtmlTableRow oRow = new HtmlTableRow();
            oRow.ID = "row_" + iLine.ToString();
            oRow.Attributes["class"] = "listrow2";

            HtmlTableCell oCellM = new HtmlTableCell();            
            oCellM.Align = "left";
            HtmlInputHidden hidden = new HtmlInputHidden();
            hidden.Value = dRow["Qst_A2_10"].ToString();
            if (i != 0)
                oCellM.Attributes["class"] = "pformright";
            oCellM.Attributes["width"] = "98%";
            oCellM.InnerHtml = "<nobr>" + (i + 1).ToString() + "、" + dRow["Qst_A2_30"].ToString() + "</nobr>"; 
            oCellM.Controls.Add(hidden);
            oRow.Cells.Add(oCellM);
            tbProduct.Rows.Add(oRow); 
            HtmlTableRow oRow1 = new HtmlTableRow();
            HtmlTableCell oCellXX = new HtmlTableCell(); 
            QuestionBLL Bll = new QuestionBLL();
            DataSet dSetX = Bll.GetQuestion(dRow["Qst_A2_10"].ToString(), "1");
            string stype = "";
            int flag = 0;
            if (dSetX.Tables[0].Rows.Count > 0)
            { 
                int newLine = 0;
                for (int j = 0; j < dSetX.Tables[0].Rows.Count; j++)
                    { 
                        flag++;
                        //if (j != 0)
                        //    newLine++;                        
                        DataRow dRowx = dSetX.Tables[0].Rows[j];
                        string iNum = Bll.GetAnswerMes(dRowx["Qst_A3_10"].ToString(), userId);
                        string checkInfo = (iNum != "") ? "checked" : ""; 
                        HtmlGenericControl span = new HtmlGenericControl();
                        span.InnerHtml = dRowx["Qst_A3_40"].ToString()+"&nbsp;";
                        if (dRowx["Qst_A2_50"].ToString() == "0")
                        {
                            stype = "radio";
                            HtmlInputRadioButton rb = new HtmlInputRadioButton();
                            rb.Attributes.Add("name", stype + "_" + iLine); 
                            rb.Attributes.Add("value", dRowx["Qst_A3_10"].ToString());
                            if (checkInfo!="")
                                rb.Attributes.Add("checked", checkInfo);
                            oCellXX.Controls.Add(rb);
                        }
                        else
                        {
                            stype = "checkbox";
                            HtmlInputCheckBox cb = new HtmlInputCheckBox();
                            cb.ID = stype + "_" + iLine; 
                            cb.Attributes.Add("value", dRowx["Qst_A3_10"].ToString());
                            if (checkInfo != "")
                                cb.Attributes.Add("checked", checkInfo);
                            oCellXX.Controls.Add(cb);
                        }
                        oCellXX.Controls.Add(span);
                        oCellXX.Align = "left";
                        oCellXX.Attributes["width"] = "100%";
                        oCellXX.Attributes["class"] = "pformright";
                        if (dRowx["Qst_A2_70"].ToString() != "" && dRowx["Qst_A2_70"].ToString() != "0")
                        {
                            if (flag % int.Parse(dRowx["Qst_A2_70"].ToString())==0)
                            {
                                oRow1.Cells.Add(oCellXX);
                                tbProduct.Rows.Add(oRow1);
                                oCellXX = new HtmlTableCell();
                                oRow1 = new HtmlTableRow();
                                //newLine = 0;
                            }
                        }
                        if (flag == dSetX.Tables[0].Rows.Count)
                        {
                            oRow1.Cells.Add(oCellXX);
                            tbProduct.Rows.Add(oRow1);
                        }
                    } 
            }
            if (dRow["Qst_A2_60"].ToString() == "1")
            {
                HtmlTableRow oRow2 = new HtmlTableRow();
                HtmlTableCell oCellQT = new HtmlTableCell();
                oCellQT.Align = "left";
                oCellQT.ColSpan = dSetX.Tables[0].Rows.Count;
                oCellQT.Attributes["class"] = "pformright";
                oCellQT.Attributes["width"] = "98%";
                string Options = Bll.GetOptionsMessages(dRow["Qst_A2_10"].ToString(), userId);
                oCellQT.InnerHtml = "<nobr><lable style='vertical-align:middle'>其它:</lable> " + "<textarea id='txtareaQT_" + iLine + "' style='width:94%' rows='2' class='textinput'>" + Options + "</textarea></nobr>";
                oRow2.Cells.Add(oCellQT);
                tbProduct.Rows.Add(oRow2);
            }
        } 
        private int Count
        {
            get
            {
                // return (int)ViewState["curCount"];
                return int.Parse(_hidCurCount.Value);
            }
            set
            {
                //ViewState["curCount"] = value;
                _hidCurCount.Value = value.ToString();
            }
        }
        protected void lnkFirst_Click(object sender, EventArgs e)
        {
            if (ViewState["MSGIDS"] != "")
            {
                string msgIds = ViewState["MSGIDS"].ToString();
                string[] arr = msgIds.Split(',');
                _hidsRecid.Value = arr[0];
                _hidCurCount.Value = "1";
                initializeFirstPage(_hidsRecid.Value, _hidCurCount.Value);
            }
        }

        protected void lnkUp_Click(object sender, EventArgs e)
        {
            Count--;
            if (ViewState["MSGIDS"] != "")
            {
                string msgIds = ViewState["MSGIDS"].ToString();
                string[] arr = msgIds.Split(',');
                for (int i = 1; i <= arr.Length; i++)
                {
                    if (arr[i - 1] == _hidsRecid.Value)
                    {
                        _hidCurCount.Value = (i - 1).ToString();
                        _hidsRecid.Value = arr[i - 2];
                        break;
                    }
                }
                initializeFirstPage(_hidsRecid.Value, _hidCurCount.Value);
            }
        }

        protected void lnkDown_Click(object sender, EventArgs e)
        {
            Count++;
            if (ViewState["MSGIDS"] != "")
            {
                string msgIds = ViewState["MSGIDS"].ToString();
                string[] arr = msgIds.Split(',');
                for (int i = 1; i <= arr.Length; i++)
                {
                    if (arr[i - 1] == _hidsRecid.Value)
                    {
                        _hidCurCount.Value = (i + 1).ToString();
                        _hidsRecid.Value = arr[i];
                        break;
                    }
                }
                initializeFirstPage(_hidsRecid.Value, _hidCurCount.Value);
            }
        }

        protected void lnkLast_Click(object sender, EventArgs e)
        {
            if (ViewState["MSGIDS"] != "")
            {
                string msgIds = ViewState["MSGIDS"].ToString();
                string[] arr = msgIds.Split(',');
                _hidsRecid.Value = arr[arr.Length - 1];
                initializeFirstPage(_hidMaxCount.Value, _hidMaxCount.Value);
            }
        }
        #region Ajax Methods
        [global::System.Web.Services.WebMethod]
        public static string DoSave(string strJson)
        {
            QuestionBLL Bll = new QuestionBLL();
            MsQuentions ms = (MsQuentions)JavaScriptConvert.DeserializeObject(strJson, typeof(MsQuentions));
            List<string> lstResult = Bll.DoSaveQuestionMessage(ms);
            var newObj = new { result = lstResult[0], message = lstResult[1]};
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        #endregion
    }
}
